<?php

namespace App\Http\Controllers\Admin\Exts;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\Order;
use App\Qingwuit\Models\OrderSettlement;
use Illuminate\Http\Request;
class RidersController extends Controller
{
    protected $modelName = 'Riders'; #对应Laravel 生成的 Model文件


//司机填写配送时间
    public function fill(Request $request){

        $order_id = $request->order_id??'';
        $time = $request->time??'';
        $order = Order::find($order_id);
        if($order){
            $order->deliverytime = $time;
            $order->save();
            return response()->json(['message' => '修改成功']);
        }
            return response()->json(['message' => '修改失败'], 500);
    }

    //更改订单状态接口
        public function changeStatus(Request $request)
    {
        $order_id = $request->order_id??'';
        $status   = $request->status??'';
        $code   = $request->code??'';
        if (!$code) return $this->formatError('取件码不能为空');
        $order = Order::find($order_id);
        if ($status == 2){
            if ($code != $order->pickup_code){
                return $this->formatError('取件码错误');
            }
        }else if ($status == 3){
            if ($code != $order->legal_code){
                return $this->formatError('送货码错误');
            }
        }
        if($order){
            $order->order_status = $status;
            $order->save();
            return $this->format('修改成功');
        }else{
            return $this->formatError('修改失败');
        }
    }

    public function index(Request $request)
    {

//        echo 111;
        try {
            $rs = $this->getService($this->serviceName)
                ->setUserConfig(['setUser'=>($this->isSuper?false:$this->setUser),'auth'=>$this->auth,'belongName'=>$this->belongName])
                ->getPageData($this->modelName);
            return $this->handle($rs);
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
